package com.manor.main.activity;

import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.manor.common.CommonAppConfig;
import com.manor.common.Constants;
import com.manor.common.activity.AbsActivity;
import com.manor.common.event.LoginInvalidEvent;
import com.manor.common.utils.ActivityManager;
import com.manor.common.utils.DataCleanManager;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.SpUtil;
import com.manor.common.utils.SpUtils;
import com.manor.im.utils.PushUtils;
import com.manor.main.R;
import com.mob.adsdk.AdSdk;
import com.mob.gamesdk.GameSdk;
import com.tencent.qcloud.tim.uikit.TUIKit;
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by cxf on 2017/10/9.
 * 登录失效的时候以dialog形式弹出的activity
 */
@Route(path = RouteUtil.PATH_LOGIN_INVALID)
public class LoginInvalidActivity extends AbsActivity implements View.OnClickListener {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login_invalid;
    }

    @Override
    protected void main() {
        TextView textView = (TextView) findViewById(R.id.content);
        String tip = getIntent().getStringExtra(Constants.TIP);
        textView.setText(tip);
        findViewById(R.id.btn_confirm).setOnClickListener(this);

        EventBus.getDefault().post(new LoginInvalidEvent());
        CommonAppConfig.getInstance().clearLoginInfo();
        //退出IM
        TUIKit.logout(new IUIKitCallBack() {
            @Override
            public void onSuccess(Object data) {

            }

            @Override
            public void onError(String module, int errCode, String errMsg) {

            }
        });
        //友盟统计登出
        MobclickAgent.onProfileSignOff();
        AdSdk.getInstance().setUserId(null);
    }

    @Override
    public void onClick(View v) {
        logout();
    }

    /**
     * 退出登录
     */
    private void logout() {
        SpUtils.getInstance(mContext).clear();
        SpUtil.getInstance().clear();
        CommonAppConfig.getInstance().clearLoginInfo();
        DataCleanManager.clearAllCache(this);
        //退出IM
        TUIKit.logout(new IUIKitCallBack() {
            @Override
            public void onSuccess(Object data) {
                TUIKit.unInit();
            }

            @Override
            public void onError(String module, int errCode, String errMsg) {

            }
        });
        PushUtils.deleteToken(mContext);
        //友盟统计登出
        MobclickAgent.onProfileSignOff();
        AdSdk.getInstance().setUserId(null);
        GameSdk.getInstance().setUserId(null);
        ActivityManager.getInstance().finishOtherActivities(LoginInvalidActivity.class,false);
        ARouter.getInstance().build(RouteUtil.PATH_LAUNCHER)
                .navigation();
        finish();
    }



    @Override
    public void onBackPressed() {
    }
}
